﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using EncuentraloSilverlight.ServiceReference2;
using System.Windows.Browser;


namespace EncuentraloSilverlight
{
	public partial class UserControl1 : Page
	{
		public UserControl1()
		{
			// Required to initialize variables
			InitializeComponent();

            string parameter = Cookie.GetCookie("EncuentraloSearch");
            if (parameter != null && parameter.Length > 0)
            {
                SearchProduct(parameter, Datagrid_ProductResults, TextBlock_UserSearch);
            }

            Cookie.SetCookie("EncuentraloSearch", "");

		}
        private void KeyUp_TextBox_CommonExpressSearch(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                NavigationService.Navigate(new Uri("/Views/BusquedaExpress.xaml", UriKind.Relative));
                SearchProduct(TextBox_CommonExpressSearch.Text, Datagrid_ProductResults, TextBlock_UserSearch);
            }
        }

        private void Click_Button_CommonExpressSearch(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/BusquedaExpress.xaml", UriKind.Relative));
            SearchProduct(TextBox_CommonExpressSearch.Text, Datagrid_ProductResults, TextBlock_UserSearch);
        }
        public void SearchProduct(string searchString, DataGrid Datagrid_ProductResults, TextBlock TextBlock_UserSearch)
        {
            Service2Client webservice = new Service2Client();

            ObservableCollection<Product> searchResults = new ObservableCollection<Product>();
            webservice.FindAllProductsCompleted += (obj, eventarg) =>
            {
                ObservableCollection<Product> products = eventarg.Result;

                
                foreach (Product i in products)
                {
                    if (i.Name.ToLower().Contains(searchString.ToLower()))
                    {
                        searchResults.Add(i);
                    }
                }

                if (searchResults.Count != 0)
                {
                    LinkProductSeller(searchResults);

                }
                else
                    Datagrid_ProductResults.ItemsSource = null;
            };
            
            webservice.FindAllProductsAsync();
            
        }

        private void LinkProductSeller(ObservableCollection<Product> products)
        {
            List<DireccionDeVendedor> templist = new List<DireccionDeVendedor>();
            Service2Client webservice = new Service2Client();
            webservice.FindAllSellersCompleted += (objj, eventargg) =>
            {
                ObservableCollection<Seller> vendedores = eventargg.Result;

                foreach (Product prod in products)
                {
                    Seller seller = null;
                    foreach (Seller sell in vendedores)
                    {
                        if (sell.SellerID == prod.SellerID)
                        {
                            seller = sell;
                            break;
                        }
                    }

                    DireccionDeVendedor st = new DireccionDeVendedor() { Vendedor = seller.Name, Direccion = seller.Address.City + ", " + seller.Address.Commune + ", " + seller.Address.Street + " " + seller.Address.Number, Descripcion = prod.Description, Precio = "$" + prod.Price.ToString(), Producto = prod.Name };
                    templist.Add(st);
                }
                
                Datagrid_ProductResults.ItemsSource = templist;
            };

            webservice.FindAllSellersAsync();
        }

	}
}